package gui;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import conference.Author;
import conference.Conference;
import conference.Manuscript;
import conference.User;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

/**
 * Author Submit Manuscript
 * @author Katie Park & Allison Graham
 * @version Fall 2011
 * @author Dan Garrison
 * Added switch user and conference functionality.
 * Added universal save and quit buttons to ensure
 * uniform behavior throughout the application.
 */
public class AuthorSubmitManuscriptPanel extends Observable{
	private static final String TITLE = "Submit a Manuscript";
	JFileChooser fileChooser;
	private JPanel myPanel;
	public static int BORDER_SPACE = 20;
	private Conference myConference;
	private User myUser;
	private File myFile;
	
	/**
	 * Creates a Author submit Manuscript Panel
	 */
    public AuthorSubmitManuscriptPanel(final Conference theConference, final User theUser) {	
    	myPanel = new JPanel(getBorderLayout());
		myConference = theConference;
    	myUser = theUser;
    	
    	//Display the window.
    	setUpContents();
    	myPanel.setOpaque(true);
    }
	
    /**
     * Returns the panel for this object.
     * @return This panel as JPanel.
     */
    public JPanel getPanel() {
    	JPanel contentPanel = new JPanel(new BorderLayout());
    	JPanel quitSavePanel = new JPanel(getBorderLayout());
    	
    	JPanel westPanel = new JPanel();
    	JLabel blankLabel = new JLabel("     ");
    	westPanel.add(GuiController.getSwitchConferenceButton());
    	westPanel.add(GuiController.getSwitchUserButton());
    	westPanel.add(GuiController.getQuitButton());
    	westPanel.add(GuiController.getSaveButton());

    	westPanel.add(blankLabel, BorderLayout.EAST);
    	quitSavePanel.add(westPanel, BorderLayout.EAST);
    	
    	//add Panels to contentPanel
    	contentPanel.add(myPanel, BorderLayout.CENTER); 
    	contentPanel.add(quitSavePanel, BorderLayout.SOUTH);
    	
    	return contentPanel;    	
    }
    
    /**
     * Creates and sets up components of JPanel.
     */
    private void setUpContents() { 
    	JPanel northPanel = new JPanel(getBorderLayout());
    	JPanel centerPanel = new JPanel(getGridLayout());
    	JPanel southPanel = new JPanel(new FlowLayout());
    	JPanel eastPanel = new JPanel(new FlowLayout());
    	JPanel westPanel = new JPanel(new FlowLayout());
    	
    	//setup eastPanel
    	JLabel blankPanel = new JLabel("      ");
    	eastPanel.add(blankPanel);
    	
    	//setup westPanel
    	westPanel.add(blankPanel);
    	
    	//setup northPanel
    	JLabel previousManLabel = new JLabel("Previously submitted manuscripts: ");
    	northPanel.add(blankPanel, BorderLayout.WEST);
    	northPanel.add(previousManLabel, BorderLayout.CENTER);
    	
    	
    	//setup centerPanel
    	Manuscript[] myMans = myConference.getManuscriptByAuthor((Author)myUser);
    	for (int i = 0; i < myMans.length; i++) {
    		centerPanel.add(getNewButton(myMans[i]));
    	}
    	
    	//setup southPanel
    	final JTextField myFilePathField = new JTextField(20);
       
        JButton chooseFileButton = new JButton("Choose File");
        chooseFileButton.addActionListener(	
        		new ActionListener(){
        			public void actionPerformed(ActionEvent theEvent) {
        				FileNameExtensionFilter filter = new FileNameExtensionFilter("txt & text", "txt", "text");
        				JFileChooser fileChooser = new JFileChooser();
        				fileChooser.setFileFilter(filter);
        				
        				int returnVal = fileChooser.showOpenDialog(null);
        				
        				if(returnVal == JFileChooser.APPROVE_OPTION) {
        					myFilePathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
        				}
        			}
        });
       
        final JTextField titleField = new JTextField(10);
        JLabel titleLabel = new JLabel("Title: ");
        
        
      //Create submit Button
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(	
        		new ActionListener(){
        			public void actionPerformed(ActionEvent theEvent) {
        				System.out.println("Submit button was clicked");
        				
        				if (myFilePathField.getText().isEmpty()){
        					JOptionPane noFilePathPane = new JOptionPane();
        					JOptionPane.showMessageDialog(noFilePathPane, "A file has not yet been choosen.");
        				}
        				else if (titleField.getText().isEmpty()){
        					JOptionPane noTitlePane = new JOptionPane();
        					JOptionPane.showMessageDialog(noTitlePane, "Title should not be empty.");
        				}
        				else {
        					//create Manuscript
        					myFile = new File(myFilePathField.getText());
        					Manuscript newManuscript = new Manuscript((Author)myUser, myFile, titleField.getText());
        					
        					//submit manuscript
        					myConference.submitManuscript(newManuscript);
        					
        					//notify to repaint
        					setChanged();
        					notifyObservers(State.AUTHOR_SUBMITS);
        				}
			}
        });
        
        southPanel.setBorder(BorderFactory.createEmptyBorder(BORDER_SPACE,BORDER_SPACE,BORDER_SPACE,BORDER_SPACE));
        southPanel.add(titleField, FlowLayout.LEFT);
        southPanel.add(titleLabel, FlowLayout.LEFT);        
        southPanel.add(submitButton, FlowLayout.LEFT);
        southPanel.add(myFilePathField, FlowLayout.LEFT);
        southPanel.add(chooseFileButton, FlowLayout.LEFT);
        
        //setup myPanel
        myPanel.add(northPanel, BorderLayout.NORTH);
        myPanel.add(centerPanel, BorderLayout.CENTER);
        myPanel.add(southPanel, BorderLayout.SOUTH);  
        myPanel.add(eastPanel, BorderLayout.EAST);
        myPanel.add(westPanel, BorderLayout.WEST);
    }
    
    /**
     * Creates a new button for manuscripts.
     * @param title The title of the manuscript.
     * @return A new button as JButton.
     */
    private JButton getNewButton(final Manuscript theManuscript) {
		JButton button = new JButton(theManuscript.getTitle());
		button.addActionListener(
				new ActionListener() {
					public void actionPerformed(final ActionEvent theEvent) {
						final JOptionPane deleteQuestionPane = new JOptionPane();
						int result = deleteQuestionPane.showConfirmDialog(null, "Are you sure you want to delete this manuscript?", "Delete", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
						if (result == 0) {
							myConference.deleteManuscript(theManuscript);
							setChanged();
							notifyObservers(State.AUTHOR_SUBMITS);
						}
					}
				}
		);
		return button;
    }

	/**
     * Sets up the border layout.
     * @return BorderLayout object.
     */
    private static BorderLayout getBorderLayout() {
    	BorderLayout borderLayout = new BorderLayout();
    	borderLayout.setHgap(BORDER_SPACE);
    	return borderLayout;
    }
    
    private static GridLayout getGridLayout() {
    	GridLayout myGridLayout = new GridLayout(0,1);
    	myGridLayout.setHgap(BORDER_SPACE * 2);
    	return myGridLayout;
    }
    
	public String getTitle() {
		return TITLE;
	}
}
